package org.zxyj.manage.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.zxyj.core.utils.Common;
import org.zxyj.manage.dao.ICourseObjectiveAnswerDao;
import org.zxyj.manage.entity.CourseObjectiveAnswer;
import org.zxyj.manage.service.ICourseObjectiveAnswerService;
import org.zxyj.system.domain.User;

@Service
public class CourseObjectiveAnswerServiceImpl implements ICourseObjectiveAnswerService{
	protected Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private ICourseObjectiveAnswerDao dao;
	
	public List<CourseObjectiveAnswer> listEntities(Map<String, Object> criteria) {
		logger.debug("listing entities refer to criteria, param: [{}]", criteria);

		if (null == criteria) {
			criteria = new HashMap<String, Object>();
		}
		return dao.find(criteria);
	}

	@SuppressWarnings("unchecked")
	@Transactional(rollbackFor = Exception.class)
	public void saveOrUpdateEntity(Map<String,Object> criteria) {
		logger.debug("persistenting entity to DB, param: criteria[{}]", criteria);
		//delete
		this.deleteEntity((List<String>)criteria.get("ids"));
		User user = (User) Common.findUserSession();
		List<String> list = (List<String>) criteria.get("strs");
		for(String str : list){
			CourseObjectiveAnswer entity = new CourseObjectiveAnswer();
			String[] strs_ = str.split("#");
			entity.setId(strs_[0]);
			entity.setTh(strs_[1]);
			entity.setDa(strs_[2]);
			entity.setCreator(user);
			entity.setOwner(criteria.get("owner").toString());
			dao.insert(entity);
			logger.debug("entity not exist in DB, then save new entity to DB, entity: {}");
		}
	}
	
	@Transactional(rollbackFor = Exception.class)
	public void deleteEntity(List<String> list) {
		logger.debug("deleting entity from DB, param: list[{}]", list);

		if (!CollectionUtils.isEmpty(list)) {
			dao.delete(list);
		}
	}
}
